<?php
namespace app\admin\controller;

use app\common\model\CourseChoose;
use app\service\CourseService;

class Course extends Base
{
    /**
     * 列表
     * @return \think\response\Json
     */
    public function course_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            $where = [];
            if ($data['title']) {
                $where['title'] = ['like', "%{$data['title']}%"];
            }
            $list = CourseService::S()->queryPage($where, $page, $size);
            $total = CourseService::S()->queryCount($where);
            return returnJson(200,'',compact('page','size','list','total'));
        }
    }

    /**
     * 编辑添加
     * @return \think\response\Json
     */
    public function course_edit()
    {
        if(request()->isPost()){
            $data = input('');
            if(!$data['title']){
                return returnJson(202,'请填写标题');
            }
            if(!$data['desc']){
                return returnJson(202,'请填写简介');
            }
            $save_data = [
                'title' => $data['title'],
                'desc' => $data['desc'],
                'week' => $data['week'],
                'type' => $data['type'],
                'start_time' => $data['start_time'],
                'end_time' => $data['end_time'],
                'is_select' => $data['is_select'],
                'num' => $data['num'],
                'update_time'=>time(),
            ];
            if($data['id']){
                $where = [
                    'id'=>$data['id']
                ];
                $res = CourseService::S()->querySave($save_data,$where);
            }else{
                $save_data['create_time'] = time();
                $res = CourseService::S()->querySave($save_data);
            }
            if($res){
                return returnJson(200,'编辑成功');
            }else{
                return returnJson(202,'编辑失败');
            }
        }
    }


    /**
     * @return \think\response\Json
     */
    public function del_course () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        \app\common\model\Course::destroy(['id'=>$input['id']]);
        return returnJson(0,'删除成功');
    }

    /**
     * 参与选课人员
     * @return \think\response\Json
     */
    public function choose()
    {
        if(request()->isPost()){
            $data = input('');
            if($data['id']){
                $where=['id'=>$data['id']];
                $choose_where=['cid'=>$data['id']];
                $course = \app\common\model\Course::getOne($where);
                if ($course['type'] == 1) {
                    //当前日期
                    $sdefaultDate = date("Y-m-d");
                    $first = 1;
                    $w = date('w', strtotime($sdefaultDate));
                    $week_start = strtotime("$sdefaultDate -" . ($w ? $w - $first : 6) . ' days');
                    $week_end = $week_start + 7*24*60*60;
                    $choose_where['update_time'] = [
                        ['>', $week_start],
                        ['<', $week_end]
                    ];
                }
                $list = CourseChoose::getAll($choose_where);
                foreach ($list as $key => $value){
                    if($value['type']==1){
                        $list[$key]['name'] = \app\common\model\Student::getOne(['id'=>$value['sid']])['name'];
                    }else{
                        $list[$key]['name'] = \app\common\model\UserTourist::getOne(['id'=>$value['sid']])['name'];
                    }
                }
                return returnJson(200,'',$list);
            }
        }
    }

   
}

